package com.example.multiactivitydemo

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import android.app.Activity

class MainActivity : AppCompatActivity() {

    private lateinit var tvResult: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        tvResult = findViewById(R.id.tvResult)

        // 按钮1：启动第一个二级Activity（正向传参）
        findViewById<Button>(R.id.btnActivity1).setOnClickListener {
            val intent = Intent(this, FirstActivity::class.java).apply {
                putExtra("data_from_main", getString(R.string.hello_world))
            }
            startActivity(intent)
        }

        // 按钮2：启动第二个二级Activity（返回结果）
        findViewById<Button>(R.id.btnActivity2).setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
            startActivityForResult(intent, REQUEST_CODE_ACTIVITY2)
        }

        // 按钮3：启动第三个二级Activity（拍照）
        findViewById<Button>(R.id.btnActivity3).setOnClickListener {
            val intent = Intent(this, ThirdActivity::class.java)
            startActivity(intent)
        }

        // 按钮4：启动第四个二级Activity（秒表）
        findViewById<Button>(R.id.btnActivity4).setOnClickListener {
            val intent = Intent(this, FourthActivity::class.java)
            startActivity(intent)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_CODE_ACTIVITY2 && resultCode == Activity.RESULT_OK) {
            val returnedData = data?.getStringExtra("return_data")
            tvResult.text = returnedData ?: getString(R.string.hello_world)
        }
    }

    companion object {
        private const val REQUEST_CODE_ACTIVITY2 = 1001
    }
}